<!--
 * @Description: 单位管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="63"
        :add-active="addActive"
        :del-active="delActive"
        :has-edit="false"
        :has-add="false"
        :has-save="false"
        :has-cancel="false"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="92%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="inventoryTaskName" label="任务/记录名称">
          <template
            v-if="scope.row.inventoryRecordNo"
            slot-scope="scope"
          >{{ scope.row.inventoryRecordName || scope.row.inventoryTask.inventoryTaskName }}</template>
        </el-table-column>
        <el-table-column prop="warehouseName" label="盘点仓库" />
        <el-table-column label="查看产品">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="checkProduct(scope.row)"
            >
              <i class="el-icon-view"></i> 查看产品
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="personName" label="盘点人">
          <template
            v-if="scope.row.inventoryRecordNo"
            slot-scope="scope"
          >{{ scope.row.personName || scope.row.inventoryTask.personName }}</template>
        </el-table-column>
        <el-table-column prop="executionDate" label="执行日期" />
        <el-table-column label="盘点结果">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="checkResult(scope.row)"
            >
              <i class="el-icon-view"></i> 查看产品
            </el-button>
          </template>
        </el-table-column>
        <el-table-column width="140" label="操作">
          <template v-if="scope.row.reason" slot-scope="scope">
            <el-popover
              placement="top-start"
              title="拒绝原因"
              width="200"
              trigger="hover"
              :content="scope.row.reason"
            >
              <el-button
                slot="reference"
                class="text-btn-cursor"
                type="text"
                size="small"
              >查看原因</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="'产品详情'"
        :visible.sync="dialogProductFormVisible"
        width="40%"
      >
        <div class="product-detail">
          <div class="title">产品列表：</div>
          <div class="product-list">
            <div
              v-for="(item, index) in curProductList"
              :key="index"
              class="product-item"
            >{{ item.goodsName }}</div>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="closeProduct()">关 闭</el-button>
        </div>
      </el-dialog>
      <el-dialog
        :title="'盘点详情'"
        :visible.sync="dialogResultFormVisible"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="resultFormData"
          class="el-form"
        >
          <el-form-item label="处理日期:" prop="executionDate" class="w100p">
            <el-date-picker
              v-model="resultFormData.executionDate"
              disabled
              align="right"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>
          <el-form-item label="盘点详情:" prop="curProductList" class="w100p">
            <div class="productList">
              <div
                v-for="(item, index) in curProductList"
                :key="index"
                class="product-item"
              >
                <div class="product-name">{{ item.goodsName }}</div>
                <div
                  class="product-value"
                >仓库剩余量: {{ item.residualValue }} {{ item.unitName }}</div>
                <div class="product-inventoryValue">
                  <el-form-item
                    label="盘点后剩余量:"
                    prop="item.inventoryValue"
                    class="w100p"
                  >
                    <el-input v-model="item.inventoryValue" disabled />
                  </el-form-item>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="closeResult()">关 闭</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/digital-operation/warehouse-check";
import warehouseManage from "@/api/basic-info-manage/warehouse-manage";
import staffManage from "@/api/digital-operation/personnel-cost-mansge/staff-manage/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixin";
export default {
  // eslint-disable-next-line vue/name-property-casing
  name: "check-history-record",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '任务名称',
          prop: 'inventoryTaskName',
          type: 'input',
        }, {
          label: '盘点仓库',
          prop: 'warehouseNo',
          type: 'select',
          options: [], // warehouseList
          optionKey: { label: 'warehouseName', value: 'warehouseNo' },
        }, {
          label: '盘点人',
          prop: 'personNo',
          type: 'select',
          options: [], // personList
          optionKey: { label: 'personName', value: 'personNo' },
        }, {
          label: '执行日期',
          prop: 'executionDate',
          type: 'date',
        }, 
      ],
      searchlist: {},
      unitList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      warehouseList: [],
      productList: [],
      personList: [],
      dialogProductFormVisible: false,
      dialogResultFormVisible: false,
      curProductList: [],
      resultFormData: {}
    };
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
    this.getWarehouseList();
    this.getPersonList();
  },
  mounted() {},
  methods: {
    checkProduct(row) {
      if (row.inventoryTaskNo) {
        this.dialogProductFormVisible = true;
        nowpagereq
          .queryAllByInventoryTaskNo(row.inventoryTaskNo)
          .then(result => {
            this.curProductList = result.data;
          });
      }
    },
    closeProduct() {
      this.curProductList = [];
      this.dialogProductFormVisible = false;
    },
    checkResult(row) {
      if (row.inventoryTaskNo) {
        this.resultFormData = row;
        this.dialogResultFormVisible = true;
        nowpagereq.getInventoryRecord(row.inventoryRecordNo).then(result => {
          this.curProductList = result.data;
        });
      }
    },
    closeResult() {
      this.curProductList = [];
      this.dialogResultFormVisible = false;
    },
    getWarehouseList() {
      let params = { entity: {} };
      warehouseManage.getWareHouseListByCondition(params).then(res => {
        this.warehouseList = res.data;
        this.setOptions(this.formConfig, 'warehouseNo', res.data)
      });
    },
    getPersonList() {
      // let params = { entity: {} };
      staffManage.getStaffListByCondition().then(res => {
        this.personList = res.data;
        let list = res.data.map(el => {
          return { ...el, personName: el.person.name }
        })
        this.setOptions(this.formConfig, 'personNo', list)
      });
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchlist
        }
      };
      nowpagereq.queryRecordPageByCondition(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    async handleEdit(row) {
      if (row.unitId) {
        this.openDialog("edit");
        this.formData = JSON.parse(JSON.stringify(row));
      }
    },
    // 添加
    add() {
      this.openDialog("add");
      this.formData = {};
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {};
    },
    // 初始化操作栏状态
    resetActionBar() {
      this.nowChanceStau = "";
      this.formData = {};
    },
    handleSelectionChange(list) {
      this.selectedPlantingUnitList = [];
      // eslint-disable-next-line no-unused-vars
      let notvalue = true;
      this.selectedPlantingUnitList = list.map(item => {
        if (item.unitNo) {
          return item.unitNo;
        } else {
          notvalue = false;
          return "";
        }
      });
    },
    delmessage() {
      nowpagereq
        .delPlantingUnitById(this.selectedPlantingUnitList)
        .then(res => {
          if (res.code === 1) {
            this.$message.success("删除成功");
            this.getTableDataList();
          } else {
            this.$message.error("删除失败");
          }
        });
    },
    handleDelete(data) {
      // nowpagereq.delPlantingUnitById([data.unitNo]).then(res => {
      //   if (res.code === 1) {
      //     this.$message.success('删除成功')
      //     this.getTableDataList();
      //   } else {
      //     this.$message.error('删除失败')
      //   }
      // })
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          nowpagereq.delPlantingUnitById([data.unitNo]).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.getTableDataList();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        nowpagereq.editPlantingUnitInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        // 新增接口
        nowpagereq.addPlantingUnitInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      // eslint-disable-next-line eqeqeq
      this.dialogTitle = type == "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss">
.df {
  display: flex;
  align-items: center;
  width: 100%;
}

.dfs {
  ::v-deep .el-form-item__content {
    flex-direction: column;
  }
  i {
    font-size: 30px;
    margin-left: 10px;
    cursor: pointer;
  }
}

.tags {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .tag-item {
    height: 30px;
    padding: 5px 10px;
    background-color: #00a1c2;
    border-radius: 8px;
    display: flex;
    align-items: center;
    margin-top: 10px;
    margin-right: 10px;

    i {
      font-size: 14px;
    }
  }
}
.productList {
  .product-item {
    display: flex;
    margin-bottom: 10px;
    .product-name {
      width: 120px;
      flex-shrink: 0;
      font-size: 16px;
    }
    .product-value {
      font-size: 16px;
      width: 200px;
      flex-shrink: 0;
    }
    .product-inventoryValue {
      width: 100%;
    }
  }
}
.product-detail {
  .title {
    font-size: 16px;
    font-weight: bold;
  }
  .product-list {
    display: flex;
    flex-wrap: wrap;

    .product-item {
      background: #00a1c2;
      color: #fff;
      text-align: center;
      padding: 8px 24px;
      margin: 10px 20px 0 0;
      border-radius: 8px;
    }
  }
}
</style>
